<?php

/**
 * Controlli Login
 */
class Router
{
    public function route()
    {
	$application = Classes_Application::getInstance();

	if( $application->auth->status != AUTH_LOGGED )
	{
	    $application->layout->disableLayout( true );

            session_destroy();
            
	    if( $_POST AND $_POST['conferma'] == 'conferma' )
	    {
		// --------------------------------
		// Convalidiamo...
		// --------------------------------
		if( !filter_var( $_POST['email'], FILTER_VALIDATE_EMAIL ) )
		    $errorMsg['email'] = '<p class="error">' . ERR_EMAIL . '</p>';

		if( !filter_var( $_POST['password'], FILTER_VALIDATE_REGEXP, array( 'options' => array( 'regexp' => '/^[0-9a-zA-Z.]{4,}$/i' ) ) ) )
		    $errorMsg['password'] = '<p class="error">' . ERR_EMPTY . '</p>';

		if( $errorMsg )
		{
		    $status = AUTH_INVALID_PARAMS;
		    $this->layout->errorMsg = $errorMsg;
		}
		else
		{
		    list( $status, $user_data ) = $application->authModel->login( filter_var( $_POST['email'], FILTER_SANITIZE_EMAIL ), $_POST['password'] );

		    if( is_null( $user_data ) )
		    {
			$status = AUTH_INVALID_PARAMS;
		    }
		    elseif( !is_null( $user_data ) )
		    {
			list( $status, $session_id ) = $application->authModel->registerSession( $user_data );

			# Cookie
			@setcookie( 'fab_session', $session_id, time() + 3600 * 365, '/' );
		    }
		}
	    }

	    $application->auth->status = $status;

	    switch( $application->auth->status )
	    {
		case AUTH_INVALID_PARAMS:
		    $application->layout->errorMsg['userAuth'] = '<div class="errore-campo">Hai inserito dati non corretti...</div>';

		    $application->layout->content = array( 'script' => 'index/login' );

		    break;

		case AUTH_LOGGED:
		case AUTH_LOGGED_IN:
		    # Reindirizza per caricare dati utente...
		    //header( 'Refresh: 3;URL=' . $_SERVER['PHP_SELF'] . '' );
//		    $hallo_msg = 'Ciao <b>' . $user->user_data['user_name'] . '.</b> Attendi il reindirizzamento';
		    header("Location: /admin");
//		    $application->layout->content = array( 'script' => 'completed' );

		    break;

		case AUTH_FAILED:
		    $errorMsg['userAuth'] = 'Fallimento durante il tentativo di connessione...';

		    ob_start();
		    include( 'html/login_form.html' );
		    $content = ob_get_contents();
		    ob_end_clean();

		    break;

		case AUTH_NOT_PERMITTED:

		    $errorMsg['userAuth'] = '<div class="errore-campo">Non hai i permessi...</div>';

		    ob_start();
		    include( 'html/login_form.html' );
		    $content = ob_get_contents();
		    ob_end_clean();

		    break;

		default:
		    $application->layout->request = array( 'admin', 'index', 'login' );
		    $application->layout->content = array( 'script' => 'index/login' );
	    } // switch

	    return;
	}
	
	$module		    = $application->getModule();
	$controller_name    = $application->getController() . 'Controller';
	$controller_name{0} = strtoupper( $controller_name{0} );
	$action		    = $application->getAction() . 'Action';

	$controller_path = APPLICATION_PATH . '/modules/' . $module . '/controllers/';

	if( !file_exists( $controller_path . $controller_name . '.php' ) )
	    throw new Exception( 'Controller inesistente: ' . $controller_name . ' in ' . $controller_path );

	require_once $controller_path . $controller_name . '.php';

	$controller = new $controller_name;

	if( !method_exists( $controller, $action ) )
	    throw new Exception( 'Action inesistente: ' . $action );

	if(  method_exists( $controller, 'init' ) )
	    $controller->init();
	
	/*
	 * Run!!
	 */
	$controller->$action();
    }

}